package utilities;

import java.io.FileNotFoundException;

import model.ProgramGraph;
import model.TransitionSystem;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/** Class for creating a graph with/without variables for a given XML document
 * @author DarkoLaptop2
 *
 */
public class GraphCreator {

	/** Method for creating a graph without variables from a given document
	 * @param doc - the document of the XML file
	 * @return a new graph without variables
	 */
	public static TransitionSystem generateGraphWithoutVariables(Document doc) {

		TransitionSystem graph = new TransitionSystem();
		NodeList nList = doc.getElementsByTagName("state");

		for (int temp = 0; temp < nList.getLength(); temp++) {

			Node nNode = nList.item(temp);
				Element eElement = (Element) nNode;
				int index = Integer.parseInt(eElement.getAttribute("id"));
				graph.addNode(index, eElement.getAttribute("name"));

				if (eElement.getElementsByTagName("initial").item(0) != null) {
					graph.setInitialNode(index);
				}
				if (eElement.getElementsByTagName("label").item(0) != null&&eElement.getElementsByTagName("label").item(0).getTextContent().toLowerCase().equals("bad")){
					graph.setBadNode(index);
				}
		}
		nList = doc.getElementsByTagName("transition");
		for (int temp = 0; temp < nList.getLength(); temp++) {
			Node nNode = nList.item(temp);
			Element eElement = (Element) nNode;
			int from = Integer.parseInt(eElement.getElementsByTagName("from")
					.item(0).getTextContent());
			int to = Integer.parseInt(eElement.getElementsByTagName("to")
					.item(0).getTextContent());
			graph.addEdge(from, eElement.getElementsByTagName("read").item(0)
					.getTextContent(), to);

		}
		return graph;

	}
	
	/** Method for generating a graph with variables for a given XML document
	 * @param doc - document from the XML file
	 * @return new graph with variables
	 */
	public static ProgramGraph generateGraphWithVariables(Document doc) {

		ProgramGraph graph = new ProgramGraph();
		NodeList nList = doc.getElementsByTagName("state");

		for (int temp = 0; temp < nList.getLength(); temp++) {

			Node nNode = nList.item(temp);
				Element eElement = (Element) nNode;
				int index = Integer.parseInt(eElement.getAttribute("id"));
				graph.addNode(index, eElement.getAttribute("name"));

				if (eElement.getElementsByTagName("initial").item(0) != null) {
					graph.setInitialNode(index);
				}
		}
		nList = doc.getElementsByTagName("transition");
		for (int temp = 0; temp < nList.getLength(); temp++) {
			Node nNode = nList.item(temp);
			Element eElement = (Element) nNode;
			int from = Integer.parseInt(eElement.getElementsByTagName("from")
					.item(0).getTextContent());
			int to = Integer.parseInt(eElement.getElementsByTagName("to")
					.item(0).getTextContent());
			String guardString=eElement.getElementsByTagName("read").item(0)
					.getTextContent();
			String actionString=eElement.getElementsByTagName("pop").item(0)
					.getTextContent();
			String transitionNameString=eElement.getElementsByTagName("push").item(0)
					.getTextContent();
			graph.addEdge(from, guardString, actionString, transitionNameString, to);

		}
		return graph;

	}
	
	/** Method for creating a new graph without variables for a given path of the XML file
	 * @param fileName - file name and path of the XML file
	 * @return a new graph without variables
	 * @throws FileNotFoundException
	 */
	public static TransitionSystem generateGraphWithoutVariables(String fileName) throws FileNotFoundException {
		DomTree dt = new DomTree(fileName);
    	Document doc = dt.ParseXml();
    	doc.getDocumentElement().normalize();
    	return generateGraphWithoutVariables(doc);
	}
	
	/** Method for creating a graph with variables for a given path to the XML file
	 * @param fileName - file name and path of the XML file
	 * @return a new graph with variables
	 * @throws FileNotFoundException
	 */
	public static ProgramGraph generateGraphWithVariables(String fileName) throws FileNotFoundException {
		DomTree dt = new DomTree(fileName);
    	Document doc = dt.ParseXml();
    	doc.getDocumentElement().normalize();
    	return generateGraphWithVariables(doc);
	}
}
